<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActionDispatch::Flash</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActionDispatch::Flash 
            
                <span class="parent">&lt; 
                    
                    <a href="../Object.html">Object</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/actionpack/lib/action_dispatch/middleware/flash_rb.html">actionpack/lib/action_dispatch/middleware/flash.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>The flash provides a way to pass temporary objects between actions.
Anything you place in the flash will be exposed to the very next action and
then cleared out. This is a great way of doing notices and alerts, such as
a create action that sets <code>flash[:notice] = &quot;Post successfully
created&quot;</code> before redirecting to a display action that can then
expose the flash to its template. Actually, that exposure is automatically
done.</p>

<pre>class PostsController &lt; ActionController::Base
  def create
    # save post
    flash[:notice] = &quot;Post successfully created&quot;
    redirect_to @post
  end

  def show
    # doesn't need to assign the flash notice to the template, that's done automatically
  end
end

show.html.erb
  &lt;% if flash[:notice] %&gt;
    &lt;div class=&quot;notice&quot;&gt;&lt;%= flash[:notice] %&gt;&lt;/div&gt;
  &lt;% end %&gt;</pre>

<p>Since the <code>notice</code> and <code>alert</code> keys are a common
idiom, convenience accessors are available:</p>

<pre>flash.alert = &quot;You must be logged in&quot;
flash.notice = &quot;Post successfully created&quot;</pre>

<p>This example just places a string in the flash, but you can put any object
in there. And of course, you can put as many as you like at a time too.
Just remember: They’ll be gone by the time the next action has been
performed.</p>

<p>See docs on the <a href="Flash/FlashHash.html">FlashHash</a> class for more
details about the flash.</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">CLASS</span>
          <a href="Flash/FlashHash.html">ActionDispatch::Flash::FlashHash</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Flash.html#method-i-call">call</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Flash.html#method-c-new">new</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">KEY</td>
            <td>=</td>
            <td class="attr-value">'action_dispatch.request.flash_hash'.freeze</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-new">
            
              <b>new</b>(app)
            
            <a href="Flash.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/654f0d6a1520fc6a0a0396e78255a9ec50594e44/actionpack/lib/action_dispatch/middleware/flash.rb#L236" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/middleware/flash.rb, line 236</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>(<span class="ruby-identifier">app</span>)
  <span class="ruby-ivar">@app</span> = <span class="ruby-identifier">app</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                  
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-call">
            
              <b>call</b>(env)
            
            <a href="Flash.html#method-i-call" name="method-i-call" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-call_source')" id="l_method-i-call_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/654f0d6a1520fc6a0a0396e78255a9ec50594e44/actionpack/lib/action_dispatch/middleware/flash.rb#L240" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-call_source" class="dyn-source">
                <pre><span class="ruby-comment"># File actionpack/lib/action_dispatch/middleware/flash.rb, line 240</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">call</span>(<span class="ruby-identifier">env</span>)
  <span class="ruby-ivar">@app</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">env</span>)
<span class="ruby-keyword">ensure</span>
  <span class="ruby-identifier">session</span>    = <span class="ruby-constant">Request</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">env</span>) <span class="ruby-operator">||</span> {}
  <span class="ruby-identifier">flash_hash</span> = <span class="ruby-identifier">env</span>[<span class="ruby-constant">KEY</span>]

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">flash_hash</span>
    <span class="ruby-keyword">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">flash_hash</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">session</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-string">'flash'</span>)
      <span class="ruby-identifier">session</span>[<span class="ruby-string">&quot;flash&quot;</span>] = <span class="ruby-identifier">flash_hash</span>.<span class="ruby-identifier">to_session_value</span>
      <span class="ruby-identifier">new_hash</span> = <span class="ruby-identifier">flash_hash</span>.<span class="ruby-identifier">dup</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">new_hash</span> = <span class="ruby-identifier">flash_hash</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">env</span>[<span class="ruby-constant">KEY</span>] = <span class="ruby-identifier">new_hash</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">if</span> (<span class="ruby-operator">!</span><span class="ruby-identifier">session</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:loaded?</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">session</span>.<span class="ruby-identifier">loaded?</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-comment"># (reset_session uses {}, which doesn't implement #loaded?)</span>
     <span class="ruby-identifier">session</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-string">'flash'</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">session</span>[<span class="ruby-string">'flash'</span>].<span class="ruby-identifier">nil?</span>
    <span class="ruby-identifier">session</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-string">'flash'</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    